// web_server.cpp
#include <WebServer.h>
#include <Preferences.h>
#include <WiFi.h>

// === ✅ 添加函数声明 ===
void saveWiFiConfig(const String& ssid, const String& password);

static WebServer server(80);
Preferences preferences;

const char* PREF_NAMESPACE = "wifi_config";
const char* PREF_SSID = "ssid";
const char* PREF_PASS = "pass";
const char* PREF_CONFIGURED = "configured";

const char* AP_SSID = "ESP32_Player";
const char* AP_PASS = "12345678";

const char INDEX_HTML[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
    <title>配置WiFi</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body { font-family: Arial; text-align: center; margin: 30px; }
        input, button { padding: 10px; margin: 10px; width: 80%; font-size: 16px; }
    </style>
</head>
<body>
    <h1>📶 配置你的WiFi</h1>
    <p>请填写你的WiFi信息</p>
    <form action="/save" method="POST">
        <input type="text" name="ssid" placeholder="WiFi名称 (SSID)" required><br>
        <input type="password" name="pass" placeholder="WiFi密码" minlength="8"><br>
        <button type="submit">连接</button>
    </form>
</body>
</html>
)rawliteral";

void handleAPRoot() {
    server.send(200, "text/html", INDEX_HTML);
}

void handleSave() {
    if (server.hasArg("ssid") && server.hasArg("pass")) {
        String ssid = server.arg("ssid");
        String pass = server.arg("pass");

        saveWiFiConfig(ssid, pass);  // ✅ 现在可以正确调用！

        server.send(200, "text/html", "<h1>✅ 保存成功！正在尝试连接...</h1><p>请等待10秒后关闭热点并尝试连接家庭网络。</p>");

        delay(2000);
        ESP.restart();
    } else {
        server.send(400, "text/plain", "Missing parameters");
    }
}

// === 函数定义保持不变 ===
void startAPWebServer() {
    WiFi.softAP(AP_SSID, AP_PASS);
    IPAddress apIP(192, 168, 4, 1);
    WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
    server.on("/", HTTP_GET, handleAPRoot);
    server.on("/save", HTTP_POST, handleSave);
    server.begin();
    Serial.println("🌐 配网热点已启动: " + String(AP_SSID));
    Serial.println("👉 请连接WiFi: " + String(AP_SSID) + " (密码: " + String(AP_PASS) + ")");
    Serial.println("👉 浏览器访问: http://192.168.4.1");
}

void handleAPClient() {
    server.handleClient();
}

bool isWiFiConfigured() {
    preferences.begin(PREF_NAMESPACE, true);
    bool configured = preferences.getBool(PREF_CONFIGURED, false);
    preferences.end();
    return configured;
}

void saveWiFiConfig(const String& ssid, const String& password) {
    preferences.begin(PREF_NAMESPACE, false);
    preferences.putBool(PREF_CONFIGURED, true);
    preferences.putString(PREF_SSID, ssid);
    preferences.putString(PREF_PASS, password);
    preferences.end();
    Serial.println("💾 WiFi 配置已保存: " + ssid);
}
void clearSavedWiFi() {
    preferences.begin(PREF_NAMESPACE, false);  // ✅ 正确命名空间
    preferences.clear();
    preferences.end();

    // ✅ 关键：清除 ESP32 内部 WiFi 缓存
    WiFi.disconnect(true);  // true = 删除所有已保存的网络
    WiFi.mode(WIFI_AP_STA); // 重置模式

    Serial.println("🗑️ 已清除 WiFi 配置并断开连接！");
}

bool getSavedWiFiConfig(String& out_ssid, String& out_password) {
    if (!isWiFiConfigured()) return false;
    preferences.begin(PREF_NAMESPACE, true);
    out_ssid = preferences.getString(PREF_SSID, "");
    out_password = preferences.getString(PREF_PASS, "");
    preferences.end();
    return !out_ssid.isEmpty();
}